package com.pap.customer.info.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.customer.agent.info.ICustomerInfoAgent;
import com.pap.customer.agent.info.dto.CustomerInfoWithDetailDTO;
import com.pap.customer.info.auto.entity.CustomerInfo;
import com.pap.obj.vo.response.ResponseVO;

@RestController
@RequestMapping("/v1/customer/customerinfo")
public class CustomerInfoController extends BaseController<CustomerInfo, ICustomerInfoAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(CustomerInfoController.class);
	
	@Resource(name = "customerInfoAgent")
	@Override
	public void setService(ICustomerInfoAgent agent) {
		super.service = agent;
	}
	
	@RequestMapping(value = "/selectwithdetails/{customerBasciInfoId}")
	public ResponseVO selectWithDetails(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String customerBasciInfoId) throws Exception {

		CustomerInfoWithDetailDTO customerInfoWithDetailDTO = service.selectWithDetails(customerBasciInfoId);
		return ResponseVO.successdata(customerInfoWithDetailDTO);
	}
	
	@RequestMapping(value = "/savewithdetails")
	public ResponseVO saveWithDetails(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "saveJSON", required = true) String saveJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		CustomerInfoWithDetailDTO e = objectMapper.readValue(saveJSON, CustomerInfoWithDetailDTO.class);

		int operationInt = service.saveWithDetails(e, clientLicenseId, loginUserId);
		return ResponseVO.successdata(operationInt);
	}
	
	@RequestMapping(value = "/updatewithdetails")
	public ResponseVO updateWithDetails(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "updateJSON", required = true) String updateJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		CustomerInfoWithDetailDTO e = objectMapper.readValue(updateJSON, CustomerInfoWithDetailDTO.class);

		int operationInt = service.updateWithDetails(e, clientLicenseId, loginUserId);
		return ResponseVO.successdata(operationInt);
	}
	
	@RequestMapping(value = "/deletewithdetails/{customerBasciInfoId}")
	public ResponseVO deleteWithDetails(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String customerBasciInfoId) throws Exception {

		int operationInt = service.deleteWithDetails(customerBasciInfoId);
		return ResponseVO.successdata(operationInt);
	}
	
}
